package com.lxw.nonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.entity.ChronicDiseasePolicy;
import com.lxw.nonghe.mapper.ChronicDiseasePolicyMapper;
import com.lxw.nonghe.service.ChronicDiseasePolicyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ChronicDiseasePolicyServiceImpl extends ServiceImpl<ChronicDiseasePolicyMapper, ChronicDiseasePolicy> implements ChronicDiseasePolicyService {

    @Override
    public Page<ChronicDiseasePolicy> getPolicyPage(int current, int size,
                                                    String diseaseName, String diseaseCode, String policyName, Byte status) {
        // 检查参数合法性
        if (current <= 0) {
            current = 1;
        }
        if (size <= 0) {
            size = 10;
        }

        Page<ChronicDiseasePolicy> page = new Page<>(current, size);
        LambdaQueryWrapper<ChronicDiseasePolicy> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(diseaseName)) {
            wrapper.like(ChronicDiseasePolicy::getDiseaseName, diseaseName);
        }

        if (StringUtils.isNotBlank(diseaseCode)) {
            wrapper.eq(ChronicDiseasePolicy::getDiseaseCode, diseaseCode);
        }

        if (StringUtils.isNotBlank(policyName)) {
            wrapper.like(ChronicDiseasePolicy::getPolicyName, policyName);
        }

        if (status != null) {
            wrapper.eq(ChronicDiseasePolicy::getStatus, status);
        }

        // 默认按创建时间倒序排列
        wrapper.orderByDesc(ChronicDiseasePolicy::getCreateTime);

        return this.page(page, wrapper);
    }

    @Override
    public List<ChronicDiseasePolicy> getActivePoliciesByDisease(String diseaseCode) {
        LambdaQueryWrapper<ChronicDiseasePolicy> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(ChronicDiseasePolicy::getDiseaseCode, diseaseCode)
                .eq(ChronicDiseasePolicy::getStatus, 1) // 状态为启用
                .le(ChronicDiseasePolicy::getEffectiveDate, new Date()) // 生效日期小于等于当前日期
                .ge(ChronicDiseasePolicy::getExpiryDate, new Date()); // 失效日期大于等于当前日期

        return this.list(wrapper);
    }

    @Override
    public Page<ChronicDiseasePolicy> getChronicDiseasePolicyPage(int current, int size, String diseaseName) {
        // 复用已有方法
        return getPolicyPage(current, size, diseaseName, null, null, null);
    }

    @Override
    public List<ChronicDiseasePolicy> getChronicDiseasePolicyByDiseaseName(String diseaseName) {
        return this.lambdaQuery()
                .eq(ChronicDiseasePolicy::getDiseaseName, diseaseName)
                .list(); // 改为 .list() 返回集合
    }

    @Override
    public Page<ChronicDiseasePolicy> queryChronicDiseasePolicy(
            ChronicDiseasePolicy queryChronicDiseasePolicy,
            int current, int size) {
        // 检查参数合法性
        if (current <= 0) {
            current = 1;
        }
        if (size <= 0) {
            size = 10;
        }

        LambdaQueryWrapper<ChronicDiseasePolicy> wrapper = new LambdaQueryWrapper<>();

        // 精确查询条件
        if(queryChronicDiseasePolicy.getPolicyId() != null) {
            wrapper.eq(ChronicDiseasePolicy::getPolicyId, queryChronicDiseasePolicy.getPolicyId());
        }

        if(queryChronicDiseasePolicy.getYear() != null) {
            wrapper.eq(ChronicDiseasePolicy::getYear, queryChronicDiseasePolicy.getYear());
        }

        // 模糊查询 - 疾病名称
        if(StringUtils.isNotBlank(queryChronicDiseasePolicy.getDiseaseName())) {
            wrapper.like(ChronicDiseasePolicy::getDiseaseName,
                    queryChronicDiseasePolicy.getDiseaseName().trim());
        }

        // 新增字段查询条件
        if(StringUtils.isNotBlank(queryChronicDiseasePolicy.getDiseaseCode())) {
            wrapper.eq(ChronicDiseasePolicy::getDiseaseCode, queryChronicDiseasePolicy.getDiseaseCode());
        }

        if(StringUtils.isNotBlank(queryChronicDiseasePolicy.getPolicyName())) {
            wrapper.like(ChronicDiseasePolicy::getPolicyName, queryChronicDiseasePolicy.getPolicyName().trim());
        }

        if(queryChronicDiseasePolicy.getStatus() != null) {
            wrapper.eq(ChronicDiseasePolicy::getStatus, queryChronicDiseasePolicy.getStatus());
        }

        // 金额和比例查询
        if(queryChronicDiseasePolicy.getCapAmount() != null) {
            wrapper.eq(ChronicDiseasePolicy::getCapAmount, queryChronicDiseasePolicy.getCapAmount());
        }

        if(queryChronicDiseasePolicy.getReimbursementRate() != null) {
            wrapper.eq(ChronicDiseasePolicy::getReimbursementRate,
                    queryChronicDiseasePolicy.getReimbursementRate());
        }

        // 设置分页参数
        Page<ChronicDiseasePolicy> page = new Page<>(current, size);

        // 执行查询
        return this.page(page, wrapper);
    }
}
